<?php

/**
 * Copyright              [HD框架] (C)2011-2012 houdunwang ，Inc. 
 * Encoding               UTF-8 
 * Version                $Id: modelControl      2012-8-26 12:47:25
 * @author                向军
 * Link                   http://www.houdunwang.com       
 * E-mail                 houdunwangxj@gmail.com
 */

class modelControl extends commonControl {

    //显示模型列表
    function index() {
        $db = M("model");
        $this->assign("model", $db->all());
        $this->display();
    }

    //添加模型
    function add_show() {
        $this->display();
    }

    //添加模型 
    function add() {
        $charset = str_replace("-", "", CHARSET);
        $db = M();
        $_POST['ptable'] = $_POST['mname']; //主表名
        //创建主表
        $sql = array();
        if ($_POST['createtable'] == 1) {
            $sql['p'] = "create table if not exists " . C("DB_PREFIX") . $_POST['ptable'] .
                    " (id int primary key auto_increment,title varchar(100),
                        addtime int(10),state tinyint(1),click int,keywords varchar(45),description varchar(200),uid int,catid smallint
                        
                       ) CHARSET " . $charset;
            $_POST['stable'] = $_POST['ptable'] . '_data'; //数据表
            $sql['s'] = "create table if not exists " . C("DB_PREFIX") . $_POST['stable'] . "(
                                id int unique,data text,allow_comment tinyint(1) default 1,template varchar(100)
                        )  CHARSET " . $charset;
        } else {
            $sql['p'] = "create table if not exists " . C("DB_PREFIX") . $_POST['ptable'] . " 
                (id int primary key auto_increment,title varchar(100),data text,
                addtime int(10),state tinyint(1),click int,keywords varchar(45),description varchar(200),uid int,catid smallint,
                template varchar(100),allow_comment tinyint(1) default 1,
                ) CHARSET " . $charset;
            $_POST['stable'] = '';
        }
        //创建数据表
        foreach ($sql as $v) {
            if ($db->query($v)) {
                $this->error("创建副数据表失败");
            }
        }

        if ($db->table("model")->add()) {
            $this->success("添加模型成功", "index");
        } else {
            $this->error("添加模型失败", "index");
        }
    }

    //删除模型
    function del() {
        $mid = $_GET['mid'];
        $db = M("model");
        if ($db->table("category")->find("mid='$mid'")) {
            $this->error("请删除此模型栏目");
        }
        $model = $db->find($mid);
        $sql = array();
        $sql['p'] = "drop table if exists " . $model['ptable'];
        if ($model['stable']) {
            $sql['s'] = "drop table  if exists " . $model['stable'];
        }
        foreach ($sql as $s) {
            $db->exe($s);
        }
        if ($db->del($mid)) {
            $this->success("删除模型成功");
        } else {
            $this->error("删除模型失败");
        }
    }

    //字段管理
    function field() {
        $mid = $_GET['mid'];
        $db = M("model_field");
        $field = $db->all("mid='$mid'");
        $this->assign("field", $field);
        $this->display();
    }

    //显示添加字段视图
    function field_add_show() {
        $db = M("model");
        $mid = $_GET['mid'];
        $this->assign("model", $db->find("mid='$mid'"));
        $this->display();
    }

    //添加字段
    function field_add() {
        $db = M("model_field");
        //添加字段SQL
        $sql = "ALTER TABLE " . C("DB_PREFIX") . $_POST['tablename'] . " ADD `" .
                $_POST['fname'] . "` " . $_POST['fieldset']['fieldtype'] . "(" . $_POST['fieldset']['fieldsize'] .
                ") default '" . $_POST['fieldset']['value'] . "'";
        $db->exe($sql);
        //fieldset[width] 
        //将字段信息转为字段串
        $_POST['fieldset'] = var_export($_POST['fieldset'], true);
        if ($db->add()) {
            $this->write_catch_field($_POST['mid']); //生成字段缓存数据
            dir::del(PATH_TEMP . '/table'); //删除表缓存
            $this->success("添加字段成功", __CONTROL__ . '/field/mid/' . $_POST['mid']);
        } else {
            $this->error("添加字段失败");
        }
    }

    //删除字段 
    function field_del() {
        
    }

    //将字段写入缓存
    function write_catch_field($mid) {
        $db = M("model_field");
        $fields = $db->all("mid='$mid'");
        $catchStr = '';
        $catchStr.="<?php if(!defined('PATH_HD'))exit;\n return ";
        $catchStr.=var_export($fields, true) . ";\n ?>";
        file_put_contents('./data/field/field_' . $mid . '.php', $catchStr);
    }

}

?>
